#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int torus_lot( const vector<vector<int> > &C );
//--
int init_c(vector< vector<int> > &C);
int print_c(vector< vector<int> > &C);
int circle(int i, int nSize);
int main()
{
    int M = 10;
    int N = 10;
    //vector C M rows N cols
    vector< vector<int> > C(M, vector<int>(N, 0));
    init_c(C);

    print_c(C);

    cout << "torus_lot: " << torus_lot(C) << endl;

    cout << "circle: " << circle(10, 8) << endl;
}

//-----------------------------------------------------------------------------
int circle(int i, int nSiz)
{
    return i < nSiz ? i : (i-nSiz);
}
int profit(const vector< vector<int> > &C, int mrow, int ncol, int iPos, int jPos, int iSiz, int jSiz)
{
    int iProfit = 0;
    int ic, jc;

    for(int i = iPos; i < iPos + iSiz; i++)
    {
        for(int j = jPos; j < jPos + jSiz; j++)
        {
            ic = circle(i, mrow);
            jc = circle(j, ncol);
            iProfit += C[ic][jc];
        }
    }
    return iProfit;
}

int max_profit_by_pos(const vector< vector<int> > &C, int mrow, int ncol, int iPos, int jPos)
{
    int m_profit = 0;
    int tmp;

    for (int i = 1; i <= mrow; i++)
    {
        for (int j = 1; j <= ncol; j++)
        {
            tmp = profit(C, mrow, ncol, iPos, jPos, i, j);
            m_profit = m_profit > tmp ? m_profit : tmp;
        }
    }
    return m_profit;
}

int torus_lot ( const vector< vector<int> > &C )
{
    int mrow = C.size();
    int ncol = C.front().size();
    int tmp;
    int m_pprofit = 0;

    for (int i = 0; i < mrow; i++)
    {
        for (int j = 0; j < ncol; j++)
        {
            tmp = max_profit_by_pos(C, mrow, ncol, i, j);
            m_pprofit = m_pprofit > tmp ? m_pprofit : tmp;
        }
    }
    return m_pprofit;
}

//-----------------------------------------------------------------------------
// create a two dimension vector, mrow rows and ncol cols
int init_c(vector< vector<int> > &C)
{
    vector< vector<int> >::iterator       iter_ii;
    vector<int>::iterator                 iter_jj;

    for(iter_ii = C.begin(); iter_ii != C.end(); iter_ii++)
    {
      for(iter_jj = (*iter_ii).begin(); iter_jj != (*iter_ii).end(); iter_jj++)
      {
         *iter_jj = rand() % 100 - 50;
      }
    }
    return 0;
}
int print_c(vector< vector<int> > &C)
{
    vector< vector<int> >::iterator       iter_ii;
    vector<int>::iterator                 iter_jj;

    for(iter_ii = C.begin(); iter_ii != C.end(); iter_ii++)
    {
      for(iter_jj = (*iter_ii).begin(); iter_jj != (*iter_ii).end(); iter_jj++)
      {
         cout << *iter_jj << endl;
      }
    }
    return 0;
}
